package com.dev.ac.web;

import com.dev.ac.annotation.NoRepeatSubmit;
import com.dev.ac.common.Result;
import com.dev.ac.service.impl.FileServiceImpl;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;

/**
 * @Author 付
 * @Date 2024-04-14 19:05
 * @Version 1.0
 * @description
 * @since 1.0
 */

@RestController
@RequestMapping("/file")
public class FileController {

	@Autowired
	FileServiceImpl fileService;

	@PostMapping("/upload")
	public Result upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		if (file.isEmpty()) {
			return Result.error("抱歉，文件不能为空");
		}
		boolean b = fileService.uploadWord(file, request);
		return Result.success();
	}

	@GetMapping("/download")
	@NoRepeatSubmit(lockTime = 30)
	public Result downLoad(String fileName, HttpServletResponse response) {
		Object path = fileService.download(fileName, response);
		System.out.println("文件内容" + path);
		return Result.success(path);
	}

	@PostMapping("/excel")
	@NoRepeatSubmit
	public Result uploadExcel(MultipartFile file, HttpServletResponse response) {
		System.out.println("上传的文件数据--->" + file);
		fileService.uploadExcel(file,response);
		return Result.success();
	}
}
